package com.qxbb.report.utils;

import com.qxbb.report.controller.LoginController;
import com.qxbb.report.pojo.ProductBarcodePic;
import com.qxbb.report.service.ProductBarcodePicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: pebble
 * @CreateTime: 2023-11-23  08:42
 * @Description: 压缩图片校验工具
 * @Version: 1.0
 */
@Component
public class ZipImgUtils {
    @Value("${qxbb.imgZipPath}")
    private String path;

    @Value("${qxbb.netWork}")
    private String netWork;

    @Value("${qxbb.picPath}")
    private String picPath;

    @Value("${qxbb.fileSuffix}")
    public String FILE_SUFFIX;
    static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ProductBarcodePicService picService;


    /**
     * 查验图片是否存在
     *
     * @param barcode
     * @param imgZipPath
     * @return
     * @throws IOException
     */
//    public static boolean isExist(String barcode, String imgZipPath) throws IOException {
//        logger.info("根据条形码验证压缩图片是否存在:{}", barcode);
//        List<String> imgList = new ArrayList<>();
//        File imgFile = new File(imgZipPath);
//        if (imgFile.isDirectory()) {
//            List<File> list = Files.walk(Paths.get(imgZipPath))
//                    .filter(Files::isRegularFile)
//                    .map(Path::toFile)
//                    .collect(Collectors.toList());
//            list.stream().forEach(file -> {
//                imgList.add(file.getName().substring(0, file.getName().indexOf(".")));
//            });
//            return imgList.contains(barcode);
//        }
//        return false;
//    }

    /**
     * 判断文件是否存在  (旧判断方法，图片在barcodePic文件夹中）
     *
     * @param barcode
     * @return boolean
     */
    public boolean imgIsExist(String barcode) throws IOException {
//        logger.info("根据条形码验证压缩图片是否存在:{}", barcode);

        File imgFile = new File(path + barcode + FILE_SUFFIX);
        return imgFile.exists();
    }


    /**
     * 根据条码查询条码平面压缩图是否存在
     *
     * @param barcode
     * @return boolean
     */
    public boolean picIsExist(String barcode) {
        boolean isExist;
        ProductBarcodePic pic = picService.selectPicByBarcode(barcode);
        isExist = !ObjectUtils.isEmpty(pic);
        return isExist;
    }

    /**
     * 根据条码拼条码图片路径（旧）
     *
     * @param barcode
     * @return
     */
    public String getImgNetworkPath(String barcode) {
        return netWork + barcode + FILE_SUFFIX;
    }

    /**
     * 根据条码查询条码平面压缩图是否存在(压缩小图）
     *
     * @param barcode
     * @return
     */
    public String getPicPath(String barcode) {
        ProductBarcodePic pic = picService.selectPicByBarcode(barcode);
        String qxPicPath;
        if (ObjectUtils.isEmpty(pic)) {
            qxPicPath = null;
        } else {
            qxPicPath = picPath + pic.getPicsPath().replaceAll("\\\\", "/");
        }
        return qxPicPath;
    }

}
